/**
 * 
 */
$(document).ready(function($){
	
	var action = "../../general/modules/requests/getAll";
	var parameters = "";
	
	getData(action, parameters).done(function(data){
		
		$(".main .body .cont .data .pinn .requ table tbody").empty();
		
		var bounds = new google.maps.LatLngBounds();
		var map = new google.maps.Map(document.getElementById("mapa"), {
			mapTypeId : 'roadmap'
		});
		var infoWindow = new google.maps.InfoWindow();
		
		$(data).each(function(i, item){
			
			$(".main .body .cont .data .pinn .requ table tbody").append(
				"<tr data-lati='" + item.latitud + "' data-long='" + item.altitud + "'>" +
				"<td><span>" + item.id + "</span></td>" +
				"<td><span>" + item.cliente.nombres + "</span></td>" +
				"</tr>"
			);
			
			var position = new google.maps.LatLng(item.latitud, item.altitud);
			
			bounds.extend(position);

			var marker = new google.maps.Marker({
				position: position,
				map: map,
				title: "Pedido nro " + item.id,
				icon: "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
			});
			
			google.maps.event.addListener(marker, 'click', (function(marker, i) {
				return function() {
					infoWindow.setContent("Pedido nro " + item.id);
					infoWindow.open(map, marker);
				}
			})(marker, i));
			
			map.fitBounds(bounds);
			
		});
		
		//--
		
		var action = "../../general/masters/markets/getAll";
		var parameters = "";
		
		getData(action, parameters).done(function(data){
			
			$(".main .body .cont .data .pinn .mark table tbody").empty();
			
			$(data).each(function(i, item){

				$(".main .body .cont .data .pinn .mark table tbody").append(
					"<tr data-lati='" + item.latitud + "' data-long='" + item.altitud + "'>" +
					"<td><span>" + item.id + "</span></td>" +
					"<td><span>" + item.descripcion + "</span></td>" +
					"</tr>"
				);
				
				var position = new google.maps.LatLng(item.latitud, item.altitud);
				
				bounds.extend(position);

				var marker = new google.maps.Marker({
					position: position,
					map: map,
					title: "Tienda " + item.descripcion,
					icon: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"
				});
				
				google.maps.event.addListener(marker, 'click', (function(marker, i) {
					return function() {
						infoWindow.setContent("Tienda " + item.descripcion);
						infoWindow.open(map, marker);
					}
				})(marker, i));
				
				map.fitBounds(bounds);
				
			});
			
		});
		
	});
	
});

$(window).keyup(function(e){
	
	if(e.which == 27){
		
		$(".main .body .loca").hide();
		
	};
	
});

function getData(url, parameters){
	
	return $.ajax({
		type: "POST",
		url: url,
		data: parameters,
		dataType: "json"
	});
	
}